<template>
  <div id="archives">
    <div class="archives-head">
      <Icon type="md-archive" />
      <span> 归档</span>
    </div>
    <div class="archives-box" :style="'height:' + setHeight()">
      <ul class="archives-list">
        <li v-for="(item, index) in archivesList"
          :key="index"
          class="archives-item"
        >
          <router-link :to="'/archive/' + item.year">
            <span class="archives-item-date" v-text="setDate(item.year)"></span>
            <span class="archives-item-count">{{item.blogList.length}}</span>
          </router-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Archives',
  computed: {
    archivesList() {
      return this.$store.getters['blog/archives']
    }
  },
  methods: {
    setDate(date) {
      return date.slice(0, 4) + '年' + date.slice(-2) + '月'
    },
    setHeight() {
      if (this.archivesList.length >= 6) {
        // 30.4 是<li>的高度 8 padding
        return (30.4*6 + 8) + 'px'
      } else {
        return (30.4*this.archivesList.length + 8) + 'px'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
#archives {
  box-shadow: 0 2px 9px rgba(31,45,61,.15);
  padding: 1rem 1.2rem;
  background: #fff;
  border-radius: 8px;
  .archives-head {
    font-size: 18px;
  }
  .archives-box {
    position: relative;
    width: 100%;
    max-height: 200px;
    overflow: hidden;
    overflow-y: scroll;
    &::-webkit-scrollbar {
      width: 0;
    }
    .archives-list {
      position: absolute;
      padding: 0.5rem 0.5rem 0;
      width: 100%;
      top: 0;
      a {
        position: relative;
        display: block;
        color: inherit;
        font-size: 16px;
        padding: 0.2rem 0;
        transition: all 0.4s;
        &:hover {
          padding: 0.2rem 0.5rem;
          background-color: rgba(40, 207, 207, 0.73);
        }
        .archives-item-date {
          display: inline-block;
          width: 80%;
        }
        .archives-item-count {
          display: inline-block;
          text-align: right;
          width: 20%;
        }
      }

    }
  }
}
</style>